package com.epfresh.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.CategoryTwoEntity;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class CategoryAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;

    public List<CategoryTwoEntity> categoryTwoEntityList;


    private AdapterView.OnItemClickListener onItemClickListener;

    public CategoryAdapter(Context context, List<CategoryTwoEntity> categoryTwoEntityList) {
        this.context = context;
        this.categoryTwoEntityList = categoryTwoEntityList;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return categoryTwoEntityList != null ? categoryTwoEntityList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_category, null, false);

            vh.tvTitle = (TextView) convertView.findViewById(R.id.tv_city);
            vh.vFlag = convertView.findViewById(R.id.v_flag);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        CategoryTwoEntity categoryTwoEntity = categoryTwoEntityList.get(position);

        if (categoryTwoEntity.isSelect()) {
            vh.vFlag.setVisibility(View.VISIBLE);
            convertView.setBackgroundResource(R.color.white);
            vh.tvTitle.setTextColor(Color.parseColor("#e83a2b"));
        } else {
            convertView.setBackgroundColor(Color.TRANSPARENT);
            vh.tvTitle.setTextColor(Color.parseColor("#434343"));
            vh.vFlag.setVisibility(View.GONE);
        }

        vh.tvTitle.setText(categoryTwoEntity.getName());

        convertView.setTag(R.id.item_key_position, position);
        convertView.setOnClickListener(this);

        return convertView;
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {


            if (onItemClickListener != null) {
                int position = (int) o;
                if(position < categoryTwoEntityList.size()) {
                    if (categoryTwoEntityList.get(position).isSelect()) {
                        //重新选中自己
                        onItemClickListener.onItemClick(null, v, position, 1);
                    } else {
                        //选择新的
                        onItemClickListener.onItemClick(null, v, position, -1);
                    }
                }
            }
            resetStatus((int) o);
        }
    }

    public void resetStatus(int position) {

        for (int i = 0; i < categoryTwoEntityList.size(); i++) {
            if (i == position) {

                categoryTwoEntityList.get(i).setSelect(true);
            } else {

                categoryTwoEntityList.get(i).setSelect(false);
            }
        }
        notifyDataSetChanged();
    }

    public String getSelectId(){
        for (int i = 0; i < categoryTwoEntityList.size(); i++) {
           if (categoryTwoEntityList.get(i).isSelect()){
               return categoryTwoEntityList.get(i).getId();
           }
        }
        return "";
    }

    public class ViewHolder {

        TextView tvTitle;
        View vFlag;

    }
}
